<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import {ref} from "vue";
import {showToast} from "vant";
import {setCurrentUser} from "@/api/user.ts";
import {userStatesStore} from "@/states/userSates.ts";

const route = useRoute()
const router = useRouter()
const editUser = ref({
  editKey: route.query.editKey,
  editName: route.query.editName,
  currentValue: route.query.currentValue
})

const userStates = userStatesStore()
const currentUser = userStates.userInfo

const onSubmit = async () => {
  const res = await setCurrentUser(
      currentUser.id,
      [editUser.value.editKey,editUser.value.currentValue]
  )
  if(res.data.code === 0 && res.data ){
    showToast('修改成功！')
    router.back()
  }else {
    showToast('修改失败！')
  }
}
</script>

<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="editUser.currentValue"
          name="用户名"
          :label="editUser.editName"
          :placeholder="`请输入${editUser.editName}`"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<style scoped>

</style>